/*
Copyright 2025 PerfXLab (Beijing) Technologies Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// #include "SparseBLAS.h"
#include <gtest/gtest.h>
#include "OpenSparseBLAS_func.h"
#include "OpenSparseBLAS_type.h"
#include "stdio.h"
#include "OpenSpB_Debug.h"
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

void generate_a_random_index(std::vector<OpenSpB_index> &a, OpenSpB_index n)
{
    for (OpenSpB_index i = 0; i < n; ++i)
    {
        a.push_back((OpenSpB_index)i);
    }
    std::random_shuffle(a.begin(), a.end());
}

#define TEST_VECTOR_SET_EXTRACT_CPP_TEST(type, T, size)      \
    TEST(Vector_Set_Extract_Test, CPP_##T)                   \
    {                                                        \
        int val_space_size = size;                           \
        std::vector<OpenSpB_index> random_a;                     \
        generate_a_random_index(random_a, 1000);             \
        type value_array[1000];                              \
        OpenSpB_init();                                          \
        OpenSpB_vector v;                                        \
        OpenSpB_vector_new(&v, OpenSpB_##T, 1000);                   \
        for (int i = 0; i < 1000; i++)                       \
        {                                                    \
            type value = mrand48() % val_space_size;         \
            value_array[i] = value;                          \
            OpenSpB_vector_set_element(v, value, random_a[i]);    \
        }                                                    \
        for (int i = 0; i < 1000; i++)                       \
        {                                                    \
            type value;                                      \
            if (OpenSpB_vector_extract_element(                   \
                    &value, v, random_a[i]) == OpenSpB_no_value) \
                value = false;                               \
            ASSERT_EQ(value_array[i], value);                \
        }                                                    \
        OpenSpB_vector_free(&v);                                 \
        OpenSpB_finalize();                                      \
    }

TEST_VECTOR_SET_EXTRACT_CPP_TEST(bool, BOOL, 2)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(int8_t, INT8, 128)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(int16_t, INT16, 32768)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(int32_t, INT32, 2147483648)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(int64_t, INT64, 2147483648)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(uint8_t, UINT8, 128)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(uint16_t, UINT16, 32768)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(uint32_t, UINT32, 2147483648)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(uint64_t, UINT64, 2147483648)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(float, FP32, 2147483648)
TEST_VECTOR_SET_EXTRACT_CPP_TEST(double, FP64, 2147483648)
#undef TEST_VECTOR_SET_EXTRACT_CPP_TEST

#define TEST_VECTOR_SET_EXTRACT_C_TEST(type, T, size)        \
    TEST(Vector_Set_Extract_Test, C_##T)                     \
    {                                                        \
        int val_space_size = size;                           \
        std::vector<OpenSpB_index> random_a;                     \
        generate_a_random_index(random_a, 1000);             \
        type value_array[1000];                              \
        OpenSpB_init();                                          \
        OpenSpB_vector v;                                        \
        OpenSpB_vector_new(&v, OpenSpB_##T, 1000);                   \
        for (int i = 0; i < 1000; i++)                       \
        {                                                    \
            type value = mrand48() % val_space_size;         \
            value_array[i] = value;                          \
            OpenSpB_vector_set_element_##T(                       \
                v, value, random_a[i]);                      \
        }                                                    \
        for (int i = 0; i < 1000; i++)                       \
        {                                                    \
            type value;                                      \
            if (OpenSpB_vector_extract_element_##T(               \
                    &value, v, random_a[i]) == OpenSpB_no_value) \
                value = false;                               \
            ASSERT_EQ(value_array[i], value);                \
        }                                                    \
        OpenSpB_vector_free(&v);                                 \
        OpenSpB_finalize();                                      \
    }

TEST_VECTOR_SET_EXTRACT_C_TEST(bool, BOOL, 2)
TEST_VECTOR_SET_EXTRACT_C_TEST(int8_t, INT8, 128)
TEST_VECTOR_SET_EXTRACT_C_TEST(int16_t, INT16, 32768)
TEST_VECTOR_SET_EXTRACT_C_TEST(int32_t, INT32, 2147483648)
TEST_VECTOR_SET_EXTRACT_C_TEST(int64_t, INT64, 2147483648)
TEST_VECTOR_SET_EXTRACT_C_TEST(uint8_t, UINT8, 128)
TEST_VECTOR_SET_EXTRACT_C_TEST(uint16_t, UINT16, 32768)
TEST_VECTOR_SET_EXTRACT_C_TEST(uint32_t, UINT32, 2147483648)
TEST_VECTOR_SET_EXTRACT_C_TEST(uint64_t, UINT64, 2147483648)
TEST_VECTOR_SET_EXTRACT_C_TEST(float, FP32, 2147483648)
TEST_VECTOR_SET_EXTRACT_C_TEST(double, FP64, 2147483648)
#undef TEST_VECTOR_SET_EXTRACT_C_TEST

#define TEST_MATRIX_SET_EXTRACT_C_TEST(type, T, size)     \
    TEST(Matrix_Set_Extract_Test, C_##T)                  \
    {                                                     \
        int val_space_size = size;                        \
        std::vector<OpenSpB_index> random_a;                  \
        std::vector<OpenSpB_index> random_b;                  \
        generate_a_random_index(random_a, 1000);          \
        generate_a_random_index(random_b, 1000);          \
        type validation_m[1000][1000];                    \
        memset(validation_m, 0,                           \
               sizeof(type) * 1000 * 1000);               \
        OpenSpB_init();                                       \
        OpenSpB_matrix m;                                     \
        OpenSpB_matrix_new(&m, OpenSpB_##T, 1000, 1000);          \
        for (int i = 0; i < 1000; i++)                    \
        {                                                 \
            type value = mrand48() % val_space_size;      \
            OpenSpB_matrix_set_element_##T(                    \
                m, value, random_a[i], random_b[i]);      \
            validation_m[random_a[i]][random_b[i]] =      \
                value;                                    \
        }                                                 \
        for (int i = 0; i < 1000; i++)                    \
        {                                                 \
            for (int j = 0; j < 1000; j++)                \
            {                                             \
                type value;                               \
                if (OpenSpB_matrix_extract_element_##T(        \
                        &value, m, i, j) == OpenSpB_no_value) \
                {                                         \
                    value = (type)0;                      \
                }                                         \
                ASSERT_EQ(validation_m[i][j], value);     \
            }                                             \
        }                                                 \
        OpenSpB_matrix_free(&m);                              \
        OpenSpB_finalize();                                   \
    }
TEST_MATRIX_SET_EXTRACT_C_TEST(bool, BOOL, 2)
TEST_MATRIX_SET_EXTRACT_C_TEST(int8_t, INT8, 128)
TEST_MATRIX_SET_EXTRACT_C_TEST(int16_t, INT16, 32768)
TEST_MATRIX_SET_EXTRACT_C_TEST(int32_t, INT32, 2147483648)
TEST_MATRIX_SET_EXTRACT_C_TEST(int64_t, INT64, 2147483648)
TEST_MATRIX_SET_EXTRACT_C_TEST(uint8_t, UINT8, 128)
TEST_MATRIX_SET_EXTRACT_C_TEST(uint16_t, UINT16, 32768)
TEST_MATRIX_SET_EXTRACT_C_TEST(uint32_t, UINT32, 2147483648)
TEST_MATRIX_SET_EXTRACT_C_TEST(uint64_t, UINT64, 2147483648)
TEST_MATRIX_SET_EXTRACT_C_TEST(float, FP32, 2147483648)
TEST_MATRIX_SET_EXTRACT_C_TEST(double, FP64, 2147483648)
#undef TEST_MATRIX_SET_EXTRACT_C_TEST